/**
 * 〈功能简述：登录方法
 * @author LiJunYi
 * @create 2019/7/3
 * @since 1.0.0
 */
package com.ljy.system.marvel_sys.system.controller;

import com.ljy.system.marvel_sys.system.model.permission.Permission;
import com.ljy.system.marvel_sys.system.model.user.User;
import com.ljy.system.marvel_sys.system.service.IPermissionService;
import com.ljy.system.marvel_sys.system.service.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("sys")
public class LoginController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IPermissionService permissionService;
    @Autowired
    HttpSession session;

    /**登录界面*/
    @RequestMapping("index")
    public String pageLogin(){
        return "login/page_login";
    }

    /** 注册页面*/
    @RequestMapping("registered")
    public String pageRegistered(){
        return "login/page_registered";
    }

    /**
    * 登录方法
    */
    @RequestMapping("login")
    @ResponseBody
    public String login(User user, Boolean rememberMe, HttpServletResponse response){
        // 处理结果
        String reMsg = "";
        // 获取当前登录用户
        Subject currentUser = SecurityUtils.getSubject();
        // 验证是否登录
        if(!currentUser.isAuthenticated()){
            // 用户名密码封装
            UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(),user.getUserPassword());
            // 配置记住我
            if(null == rememberMe){
                rememberMe = false;
            }
            token.setRememberMe(rememberMe);
            try {
                //  执行登录
                currentUser.login(token);
                reMsg="登录成功";
                // 进行菜单处理
                User user1 = userService.queryByUserName(user.getUserName());
                Permission root = permissionService.queryPermissionByUser(user1.getId());
                session.setAttribute("rootPermission", root);
                session.setAttribute("LoginUser", user1);
                return reMsg;
            }catch (IncorrectCredentialsException e){
                System.out.println("密码错误"+e.getMessage());
                reMsg="密码错误";
            }catch (LockedAccountException e){
                System.out.println("账号已被锁定");
                reMsg="账号已被锁定";
            }catch (DisabledAccountException e){
                System.out.println("账号被禁用");
                reMsg="账号被禁用";
            }catch (UnknownAccountException e){
                System.out.println("账号不存在");
                reMsg="账号不存在";
            }catch (UnauthorizedException e) {
                System.out.println("没有权限进入");
                reMsg="没有权限进入";
            }catch (AuthenticationException ae){
                System.out.println("用户名或密码错误");
                reMsg = "用户名或密码错误";
            }
            return reMsg;
        }
        return reMsg;
    }

    /**
    * 注册用户
    * */
    @RequestMapping("reg_user")
    @ResponseBody
    public String regUser(User user){
        return userService.insertUser(user);
    }
}

